﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ECGadmin.Common;
using ECGadmin.DAL;
using ECGadmin.WebUI.Attributes;
using ECGadmin.WebUI.Models;
using System.Configuration;
using ECGadmin.Utility;
using ECGadmin.WebUI.CustomHelpers;
using ECGadmin.WebUI.Infrastructure;

namespace ECGadmin.WebUI.Controllers
{
    [AdminLoggedAuthorize]
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index(int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            //neu la admin root
            var model = UserDb.GetAllUserPaging(page, itemsPerPage, ref totalPage) ?? new List<UserPagingDto>();
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            return View(model);
        }
        public ActionResult Add()
        {
            var model = new UserDto();
            ViewBag.ListStatus = CommonHelpers.SelectOptionUserStatus();
            return View(model);
        }
        [HttpPost]
        public ActionResult Add(UserDto model)
        {
            var passwordSalt = Guid.NewGuid().ToString();
            var password = model.Password.CreatePasswordHash(passwordSalt);
            model.CreateDate = DateTime.Now;
            model.Password = password;
            model.PasswordSalt = passwordSalt;
            model.RoleId = 0; //khong co quyen lam gi ca
            var result = UserDb.Add(model);
            if (result > 0)
                return RedirectToAction("Index", "User");
            return View(model);

        }

        public ActionResult Edit(int id)
        {
            var model = UserDb.GetById(id) ?? new UserDto();
            ViewBag.ListStatus = CommonHelpers.SelectOptionUserStatus();
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(UserDto model)
        {
            //var passwordSalt = Guid.NewGuid().ToString();
            //if (!string.IsNullOrEmpty(model.Password))
            //{
            //    var password = model.Password.CreatePasswordHash(model.PasswordSalt);
            //    model.Password = password;
            //}

            UserDb.Update(model);
            return RedirectToAction("Index", "User");
        }

        public ActionResult SearchUser(string keyword, int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var model = new List<UserPagingDto>();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = UserDb.GetAllUserPagingByKeyword(page, itemsPerPage, ref totalPage, keyword) ?? new List<UserPagingDto>();
            }
            else
            {
                model = UserDb.GetAllUserPaging(page, itemsPerPage, ref totalPage) ?? new List<UserPagingDto>();
            }
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            ViewBag.Keyword = keyword;
            return View(model);
        }

        public JsonResult DeleteUser(int Id)
        {
            var data = false;
            var loginModel = (AdminLoginModel)Session[SessionConfig.SessionAdminLogged];
            if (loginModel != null)
            {
                //id can xoa phai khac 0 va khac user hien tai
                if (Id != 0 && Id != loginModel.Id)
                {
                    UserDb.Delete(Id);
                    data = true;
                }
            }

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ChangePass(int id, string passOld, string passnew)
        {
            var user = UserDb.GetById(id);
            var data = false;
            if (user != null)
            {
                if (user.Password == passOld.CreatePasswordHash(user.PasswordSalt))
                {
                    user.Password = passnew.CreatePasswordHash(user.PasswordSalt);
                    data = true;
                }
            }
            UserDb.Update(user);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
